<!-- 
说明：订单表表单
作者：292303959@qq.com
日期：2018-10-17 10:5:39
版本：1.0.0
-->
<template>
  <el-dialog :title="title"
             append-to-body
             visible
             lock-scroll
             :before-close="handleClose"
             width="900px">
    <el-form ref="form"
             :model="form"
             :label-width="ui.formLabelWidth"
             :element-loading-text="ui.loadingText"
             v-loading="ui.busying">
      <el-row type="flex">
        <el-col :span="24">
          <mk-form-item label="订单编号"
                        prop="orderid"
                        :mk-le-length="32">
            {{form.orderid}}
          </mk-form-item>
        </el-col>
        <el-col :span="24">
          <mk-form-item label="会员名称"
                        prop="mname"
                        :mk-le-length="32">
            {{form.mname}}
          </mk-form-item>
        </el-col>
      </el-row>
      <el-row type="flex">
        <el-col :span="24">
          <mk-form-item label="订单金额"
                        prop="orderamount"
                        :mk-le-length="18">
            {{form.mname}}
          </mk-form-item>
        </el-col>
        <el-col :span="24">
          <mk-form-item label="订单状态 "
                        prop="orderstatus"
                        :mk-le-length="2">
            {{form.orderstatus| data-config('Z_ISFIRST')}}
          </mk-form-item>
        </el-col>
      </el-row>

      <el-row type="flex">
        <el-col :span="24">
          <mk-form-item label="下单时间"
                        prop="underdate"
                        :mk-le-length="19">
            {{form.underdate}}
          </mk-form-item>
        </el-col>
        <el-col :span="24">
          <mk-form-item label="付款时间"
                        prop="paydate"
                        :mk-le-length="19">
            {{form.paydate}}
          </mk-form-item>
        </el-col>
      </el-row>

      <el-row type="flex">
        <el-col :span="24">
          <mk-form-item label="收货时间"
                        prop="revdate"
                        :mk-le-length="19">
            {{form.revdate}}
          </mk-form-item>
        </el-col>
        <el-col :span="24">
          <mk-form-item label="完成时间"
                        prop="finishdate"
                        :mk-le-length="19">
            {{form.finishdate}}
          </mk-form-item>
        </el-col>
      </el-row>

      <el-row type="flex">
        <el-col :span="24">
          <mk-form-item label="收件人"
                        prop="recipients"
                        :mk-le-length="32">
            {{form.recipients}}
          </mk-form-item>
        </el-col>

        <el-col :span="24">
          <mk-form-item label="手机"
                        prop="cellphone"
                        :mk-le-length="15">
            {{form.cellphone}}
          </mk-form-item>
        </el-col>
      </el-row>

      <el-row type="flex">
        <el-col :span="24">
          <mk-form-item label="收货地址"
                        prop="adress"
                        :mk-le-length="500">
            {{form.province}} {{form.city}} {{form.area}} {{form.adress}}
          </mk-form-item>
        </el-col>
      </el-row>

      <el-row type="flex">
        <el-col :span="24">
          <mk-form-item label="邮政编码"
                        prop="zipcode"
                        :mk-le-length="32">
            {{form.zipcode}}
          </mk-form-item>
        </el-col>
      </el-row>
      <el-row type="flex">
        <el-col :span="24">
          <mk-form-item label="支付方式"
                        prop="paytype"
                        :mk-le-length="1">
            {{form.paytype| data-config('Z_PAYTYPE')}}
          </mk-form-item>
        </el-col>
        <el-col :span="24">
          <mk-form-item label="是否首单"
                        prop="isfirst"
                        :mk-le-length="1">
            {{form.isfirst | data-config('Z_ISFIRST')}}
          </mk-form-item>
        </el-col>
      </el-row>
      <mk-card>
        <div slot="header"
             class="clearfix">
          <span>商品明细</span>
        </div>
        <mk-table mk-index
                  :data="form.orderDetail"
                  :mk-page="false"
                  ref="table">
          <el-table-column align="left"
                           prop="pdname"
                           label="商品名称"
                           width="200" />
          <el-table-column align="left"
                           prop="brname"
                           label="系列名称"
                           width="200" />
          <el-table-column align="left"
                           prop="psname"
                           label="规格"
                           width="70" />
          <el-table-column align="left"
                           prop="detailrnumber"
                           label="下单数量"
                           width="70" />
          <el-table-column align="left"
                           prop="detailtotal"
                           label="总价"
                           width="70" />
          <el-table-column align="left"
                           prop="aftersalenum"
                           label="已售后数量"
                           width="80" />
          <el-table-column align="left"
                           prop="doing"
                           label="售后数量"
                           width="120">
            <template slot-scope="scope">
              <mk-form-item :prop="'orderDetail['+scope.$index+'].afternum'"
                            mk-type="number"
                            mk-inlay="un-minus-int"
                            :mk-le="scope.row.detailrnumber - scope.row.aftersalenum"
                            :mk-le-length="7">
                <el-input placeholder="请输入售后数量"
                          v-model="scope.row.afternum"
                          size="small"></el-input>
              </mk-form-item>
            </template>
          </el-table-column>
        </mk-table>
      </mk-card>
    </el-form>
    <div slot="footer">
      <mk-button @click="handleClose">取消</mk-button>
      <mk-button @click="onSubmit"
                 :type="2">{{btnName}}</mk-button>
    </div>
  </el-dialog>
</template>

<script>
import { MkBase } from 'element-ui'
import formData from './form-data'
import cloneDeep from 'lodash.clonedeep'
export default {
  name: 't-order-form',
  mixins: [MkBase],
  props: {
    psid: String,
    isAdd: Boolean,
    title: String,
    btnName: String,
    row: Object
  },
  async mounted () {
    if (this.isAdd === false) {
      const form = cloneDeep(this.row)
      form.orderDetail = []
      this.form = form
    }
    this.ui.busying = false
    let viewDate = await this.$get('/tOrderDetail/list.json', {orderid: this.form.orderid})
    this.form.orderDetail = viewDate.list
    // this.$set(this.form, 'orderDetail', viewDate.list)
  },
  data () {
    const form = formData()
    return {
      ui: {
        formLabelWidth: '100px',
        busying: true,
        loadingText: '正在加载'
      },
      form
    }
  },
  methods: {
    async valid () {
      if (!this.validate) {
        let fn = this.$refs.form.validate
        let nodejs = false
        this.validate = this.$promise({ fn, nodejs })
      }
      return await this.validate()
    },
    async onSubmit () {
      if (await this.valid()) {
        this.ui.loadingText = '正在保存'
        this.ui.busying = true
        try {
          let postdata = []
          this.form.orderDetail.forEach(element => {
            postdata.push({
              odid: element.odid,
              afternum: element.afternum
            })
          })
          let data = await this.$post('/tOrderDetail/afterSale.json', postdata)
          if (data) {
            this.$emit('close', true)
          }
        } finally {
          this.ui.busying = false
        }
      }
    },
    handleClose () {
      this.$emit('close')
    }
  }
}
</script>
